<?php

define("appuser_file", getcwd()."/include/appuser.dat");


function updateAllowedUsers($allowed_count){
	global $_APPUSER;
	appuser_start();
	
	$_APPUSER['ALLOWED'] = $allowed_count;
	
	appuser_end();
}

function sameUserLoggedOn(){
	global $_APPUSER;
	appuser_start();
	
	$current_logged = $_APPUSER['CURRENT_LOGGED'];
	for($i=0; $i<$current_logged; $i++){
		//if match found
		if(strcmp($_APPUSER['USER'.$i], $_SESSION['login']) == 0){
			$logged_session = $_APPUSER['SESSION'.$i];
			//if someone with the same user id but diff session id is logged in
			if(strcmp($logged_session, session_id()) != 0){
				return true;
			}
		}
	}
	return false;
}

function display_appuser(){
	global $_APPUSER;
	appuser_start();
	
	echo "Max users allowed: ".$_APPUSER['ALLOWED']."<br>";
	echo "No of users logged on: ".$_APPUSER['CURRENT_LOGGED']."<br>";
	$logged = intval($_APPUSER['CURRENT_LOGGED']);
	for($i=0; $i<$logged; $i++){
		echo $_APPUSER['USER'.$i]."<br>";
	}

}

function logout_user($user){
	global $_APPUSER;
	appuser_start();
	
	$current_logged = intval($_APPUSER['CURRENT_LOGGED']);
	$j = 0;
	if($current_logged = 1){
		$_APPUSER['USER0'] = "";
		$_APPUSER['SESSION0'] = "";
	}else{
		for($i=0; $i<current_logged; $i++){
			//echo "i = ".$i."<br>";
			if(strcmp($_APPUSER['USER'.$i], $user) == 0){
				$j = $i + 1;
			}
			//echo "j = ".$j."<br>";
			if($j < $current_logged){		
				$_APPUSER['USER'.$i] = $_APPUSER['USER'.$j];
				$_APPUSER['SESSION'.$i] = $_APPUSER['SESSION'.$j];
				//echo "user".$i." = ".$_APPUSER['USER'.$j]."<br>";
				//echo "session".$i." = ".$_APPUSER['SESSION'.$j]."<br>";
				++ $j;
			}elseif($j == $current_logged){
				$_APPUSER['USER'.$i] = "";
				$_APPUSER['SESSION'.$i] = "";
				//echo "user".$i." = 0<br>";
				//echo "session".$i." = 0<br>";
			}
		}
	}
	if($current_logged > 0){
		$_APPUSER['CURRENT_LOGGED'] = $current_logged - 1;
	}
	
	appuser_end();
	
}

function log_user($user, $session){
	global $_APPUSER;
	appuser_start();
	$current_logged = $_APPUSER['CURRENT_LOGGED'];
	$allowed = $_APPUSER['ALLOWED'];
		
	//max allowed users reached	
	if($current_logged >= $allowed){
		appuser_end();
		return false;
	}		
	
	$current_logged = $current_logged + 1;
	$_APPUSER['CURRENT_LOGGED'] = $current_logged;
	$current_index = $current_logged - 1;
	$_APPUSER['USER'.$current_index] = $user;
	$_APPUSER['SESSION'.$current_index] = $session;
	
	appuser_end();
	
}

function user_logged_on($user)
{
	global $_APPUSER;
	appuser_start();
	
	$current_logged = $_APPUSER['CURRENT_LOGGED'];
	for($i=0; $i<$current_logged; $i++){
		//if match found
		if(strcmp($_APPUSER['USER'.$i], $user) == 0){
			return true;
		}
	}
	return false;
}

function app_test(){
	global $_APPUSER;
	
	if(file_exists(appuser_file) == false){
		header("location: http://".$_SERVER['SERVER_NAME']."/simpleaccounts/app_fail.php");
	}
	
	if(strlen($_APPUSER['ALLOWED']) == 0){
		header("location: http://".$_SERVER['SERVER_NAME']."/simpleaccounts/app_corrupt.php");
	}
	
	if(strlen($_APPUSER['CURRENT_LOGGED']) == 0){
		header("location: http://".$_SERVER['SERVER_NAME']."/simpleaccounts/app_corrupt.php");
	}
}

function appuser_start(){
	
	global $_APPUSER;
	
	// read data file
	//$appuser_file = getcwd()."\include\appuser.dat";
	$file = fopen(appuser_file, "r");
	if ($file)
    {
		$data = fread($file,
		filesize(appuser_file));
		fclose($file);
	}

	// build application variables from
	//   data file 
	$_APPUSER = unserialize($data);
	app_test();

}

function appuser_end ()
{
	global $_APPUSER;
	//$appuser_file = getcwd()."\include\appuser.dat";
    // write application data to file
    $data = serialize($_APPUSER);
    $file = fopen(appuser_file, "w");
    if ($file)
    {
        fwrite($file, $data);
        fclose($file);
    }
}

?>